package com.wwj.springboot.controller;

import com.alibaba.fastjson.JSONObject;
import com.wwj.springboot.entity.Product;
import com.wwj.springboot.entity.User;
import com.wwj.springboot.service.MyProdudcerSevice;
import com.wwj.springboot.service.ProductService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpSession;
import java.util.HashMap;
import java.util.List;
import java.util.Map;


/**
 * @author wwj
 */
@Controller
@RequestMapping("product")
public class ProductController {

    @Autowired
    private ProductService productService;

    @Autowired
    private MyProdudcerSevice myProdudcerSevice;

    @RequestMapping("selectAllProduct")
    public String selectAllProduct(Model model){
        List <Product> selectAllProduct = productService.selectAllProduct();
        model.addAttribute("selectAllProduct",selectAllProduct);
        return "forward:/category/CategoryList.jsp";
    }

    @RequestMapping("selectOneProduct")
    public String selectOneProduct(int pid){
        Product selectOneProduct = productService.selectOneProduct(pid);
        return "";
    }

    @RequestMapping("updateProduct")
    @ResponseBody
    public String updateProduct(int pid, HttpSession session) {
        User user = (User) session.getAttribute("user");
        Map map = new HashMap(16);
        map.put("pid",pid);
        map.put("userid",user.getUserid());
        String s = JSONObject.toJSONString(map);
        myProdudcerSevice.send(s);
        return "";
    }

}
